/// <reference path="../GeneratedArtifacts/viewModel.js" />
/// <reference path="../Scripts/c1ls-4.0.20141.30.js" />

if (typeof c1ls === "undefined") {
    window.location.href = "http://bit.ly/c1ls-nuget";
}

var gameDays = [];

myapp.CalendarDateFilter.created = function (screen) {

    setToday(screen);

    screen.AllGames.oncollectionchange = function () {
        if (Object.getOwnPropertyNames(gameDays).length <= 1) {
            screen.AllGames.data.map(function (g) {
                gameDays[g.StartTime.toDateString()] = true;
            });

            $("#monthcal").wijcalendar({
                customizeDate: function ($dayCell, date, dayType, hover, preview) {
                    if (gameDays[date.toDateString()]) {
                        $dayCell.children("a").attr("style", "color:orange !important");
                        return true;
                    }
                }
            });
        }
    };
};

function nextDay(d) {
    return new Date(d.getTime() + (24 * 60 * 60 * 1000));
}

function setToday(screen) {
    var min = new Date('2013/10/01');
    var max = new Date('2014/04/13');
    screen.StartDate = new Date();
    screen.StartDate.setHours(0);
    screen.StartDate.setMinutes(0);
    if (screen.StartDate < min) {
        screen.StartDate = min;
    }
    else if (screen.StartDate > max) {
        screen.StartDate = max;
    }
    screen.EndDate = nextDay(screen.StartDate);
}

// http://bitly.com/c1ls-calendar
//
myapp.CalendarDateFilter.StartDate_render = function (element, contentItem) {

    $.wijmo.wijcalendar.prototype.options.wijCSS.stateDefault = "ui-btn-up-a";
    $.wijmo.wijcalendar.prototype.options.wijCSS.content = "ui-body ui-body-a";

    var div = $("<div id='monthcal' data-role='wijcalendar'/>");
    div.appendTo($(element));

    div.wijcalendar({
        selectionMode: {day: true},
        displayDate: contentItem.value,
        allowQuickPick: false,
        minDate: new Date('2013/10/01'),
        maxDate: new Date('2014/04/13'),
        customizeDate: function ($dayCell, date, dayType, hover, preview) {
            if (gameDays[date.toDateString()]) {
                $dayCell.children("a").attr("style", "color:orange !important");
                return true;
            }
        }
    });

    div.wijcalendar("selectDate", contentItem.value);
    var displayDate = true;

    div.wijcalendar({
        selectedDatesChanged: function (e, args) {
            var newValue = div.wijcalendar("getSelectedDate");
            if (contentItem.value != newValue) {
                displayDate = false;
                contentItem.value = newValue;
                contentItem.screen.EndDate = nextDay(newValue);
                displayDate = true;
            }
        }
    });

    contentItem.dataBind("value", function (newValue) {
        div.wijcalendar("unSelectAll");
        if (displayDate) {
            div.wijcalendar({ displayDate: newValue });
        }
        div.wijcalendar("selectDate", newValue);
    });
};

// http://bitly.com/c1ls-grid
//
myapp.CalendarDateFilter.SelectedGames_render = function (element, contentItem) {

    var table = $("<table/>");
    table.appendTo($(element));
    table.attr("style", "width: 400px; margin-top: 0px;");

    contentItem.value.oncollectionchange = function () {
        var grid = c1ls.getGridContent(table, contentItem);

        var time = grid.FindColumn("StartTime");
        time.dataFormatString = "t";

        table.wijgrid({
            // initialization
            columns: grid.Columns,
            data: grid.Rows(),

            // editing
            allowEditing: false,
            afterCellUpdate: grid.OnCellUpdate,

            // sorting
            allowSorting: false,
            sorting: grid.Sorting,

            // filtering
            showFilter: false,
            filtering: grid.Filtering,

            // selection
            selectionMode: "none",
            showRowHeader: grid.ShowRowHeader,
            showSelectionOnRender: false,
            cellClicked: grid.OnCellClicked,
            cellStyleFormatter: grid.OnCellFormat
        });
    };

    c1ls.renderControl(contentItem);
};
